﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.ServiceModel.DomainServices.Server;
using System.Web;
using NetBay.Core.BusinessEntities;
using NetBay.Core.Encryption.Licensing;
using NetBay.Core.Log;
using Rio.Applications.Web.Code;
using Rio.Doc.Business.Repository;
using System.Configuration;
using Rio.Framework.RioManager;
#endregion

namespace Rio.Applications.Web.Services
{
    /// <summary>
    /// Class to handle Business entities in a RIA services
    /// </summary>
    public partial class BusinessEntitiesDomainService : DomainService
    {


        [Invoke(HasSideEffects = true)]
        public LicenseTerms GetLicenseTerms()
        {
            return LicenseManager.LicenseTerms;
        }

        [Query(HasSideEffects = true)]
        public IEnumerable<User> GetAccess(string p_szUser, string p_szPassword, int p_iAuthentication, bool CreateRioScanFile = true)
        {
            User l_oUser = null;

            try
            {
                l_oUser = UserRepository.Find(p_szUser, p_szPassword, p_iAuthentication);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError(GetType().FullName + "(" + p_szUser + "," + p_szPassword + "," + p_iAuthentication + ") exception : " + ex.Message);
            }
            finally
            {
                // If Access is granted, so set the user in session and create its rioscan file
                if ((l_oUser != null) && (l_oUser.IsValid == true))
                {
                    //Set the guidid of the session
                    var L_sRefreshScanGuid = Guid.NewGuid().ToString("N");
                    l_oUser.RefreshScanGuid = L_sRefreshScanGuid;

                    // Get his culture
                    l_oUser.CTX_Culture = GetCulture();

                    // Get information about client scan installed
                    l_oUser.CTX_ClientScan = GetPcApplicationStatusInternal("RIOSCAN", l_oUser.Login).Value;

                    // Get storage for images
                    l_oUser.CTX_ParameterStorage = SettingsManager.cStoreSettings.Parameter_Storage;

                    // Get storage for images
                    l_oUser.CTX_ParameterStorageThumbnail = SettingsManager.cStoreSettings.Thumbnail_Directory;

                    //Default Organization
                    l_oUser.CTX_DefaultOrganization = SettingsManager.cStoreSettings.Parameter_EntityDeparture;

                    //Default Type
                    l_oUser.CTX_DefaultType = SettingsManager.cStoreSettings.Parameter_FolderTypeDeparture;

                    //Default display mode
                    l_oUser.CTX_DisplayMode = SettingsManager.cStoreSettings.Thumbnail_DefaultDisplayMode;

                    //Default viewer parameter fit to heigh
                    l_oUser.CTX_FitToheight = SettingsManager.cStoreSettings.Parameter_Viewer_FitToHeigh;

                    //Default viewer parameter Zoom step
                    l_oUser.CTX_ZoomStep = SettingsManager.cStoreSettings.Parameter_Viewer_ZoomStep;

                    // License key for WCF call
                    l_oUser.LicenseKey = ConfigurationManager.AppSettings.Get("LicenseKey");

                    HttpContext.Current.Session[ParameterSession.CurrentUser.szGetDescription()] = l_oUser;

                    // Add a historic of user action
                    AddActionHistoric((int)Constant.UserActions.CONNEXION, 0, 0, null, null);
                    
                    if (CreateRioScanFile)
                        CreateRioscanFileUser(l_oUser.UserId, l_oUser.LastName, l_oUser.FirstName, AppDomain.CurrentDomain.BaseDirectory + @"\Rioscan", L_sRefreshScanGuid);
                }
            }
            return new List<User> { l_oUser };
        }
    }
}
